// 重写数组方法
// 获取原来的数组方法
let oldArrayProtoMethods = Array.prototype;

// 继承
export let ArrayMethods = Object.create(oldArrayProtoMethods);

// 劫持
let methods = ["push", "pop", "shift", "unshift", "splice"];

methods.forEach((item) => {
  ArrayMethods[item] = function (...args) {
    console.log("劫持数组");
    let result = oldArrayProtoMethods[item].apply(this, args);
    // console.log(args);
    // 数组追加对象元素的情况[{b: 9}]
    let inserted;
    switch (item) {
      case "push":
      case "unshift":
        inserted = args;
        break;
      case "splice":
        inserted = args.splice(2); // arr.splice(0,1, {a: 6})
        break;
    }
    console.log(inserted);
    let ob = this.__ob__
    if(inserted) {
      ob.observeArray(inserted)
    }
    return result;
  };
});
